package com.informatics.polymer.server;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import nu.xom.Document;

import org.apache.commons.io.FileUtils;
import org.restlet.Context;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;
import org.semanticweb.owl.model.OWLOntologyChangeException;
import org.semanticweb.owl.model.OWLOntologyCreationException;

/**
 * Restlet to convert OWL file to XML.
 * 
 * @author ed
 * @version 1.0
 */
public class OWLToXMLConverterResource extends Resource {
	private String ontologyURI;
	private String ontologyName;

	/**
	 * 
	 * @param uriValue
	 *            - URI of the OWL ontology file.
	 */
	private void setURI(final String uriValue) {
		this.ontologyURI = uriValue;
	}

	/**
	 * 
	 * @return URI.
	 */
	private String getURI() {
		return ontologyURI;
	}

	/**
	 * 
	 * @param name
	 *            - File name.
	 */
	private void setName(final String name) {
		this.ontologyName = name;
	}

	/**
	 * 
	 * @return Name of ontology.
	 */
	private String getName() {
		return ontologyName;
	}

	/**
	 * Initiate the restlet.
	 */
	@Override
	public void init(Context context, Request request, Response response) {
		super.init(context, request, response);
		getVariants().add(new Variant(MediaType.TEXT_PLAIN));
		this.setModifiable(true);
	}

	/**
	 * Method allows forms to be posted to restlet.
	 */
	@Override
	public boolean allowPost() {
		return true;
	}

	/**
	 * Constructs OWLToXMLConverterResource.
	 * 
	 * @param context
	 * @param request
	 * @param response
	 */
	public OWLToXMLConverterResource(Context context, Request request,
			Response response) {
		super(context, request, response);
		getVariants().add(new Variant(MediaType.TEXT_PLAIN));
	}

	/**
	 * Method deals with the form posted to the restlet.
	 */
	@Override
	public void post(Representation entity) {
		Form form = new Form(entity);
		setURI(form.getValues("OntologyURI"));
		setName(form.getValues("OntologyName1"));
		// Write out to a file
		String f = "/" + getName() + ".xml";
		String curDir = new String(System.getProperty("user.dir"));
		File ontologyXml = new File(curDir + f);
		OWL2XMLConverter owl2XmlConverter = new OWL2XMLConverter();
		try {
			URL ontologyURL = new URL(getURI());
			Document doc = owl2XmlConverter.convert(ontologyURL);
			FileUtils.writeStringToFile(ontologyXml, doc.toXML());
		} catch (OWLOntologyCreationException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// getResponse().setEntity(str);
		catch (OWLOntologyChangeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// Respond to GET
	@Override
	public Representation represent(Variant variant) throws ResourceException {
		Representation resource = null;
		if (!MediaType.TEXT_PLAIN.includes(variant.getMediaType())) {
			throw new ResourceException(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
		} else {
			StringBuffer sb = new StringBuffer();
			try {
			} catch (Exception e) {
				e.printStackTrace();
			}
			resource = new StringRepresentation(sb.toString(),
					MediaType.TEXT_PLAIN);
			return resource;
		}
	}

}
